import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

class ImpLess implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}

public class Test {

    public static int[] topk(int[] array,int k) {
        PriorityQueue<Integer> p1 = new PriorityQueue<>(k,new ImpLess());

        for (int i = 0; i < k; i++) {
            p1.offer(array[i]);
        }

        for (int i = k; i < array.length; i++) {
            if(p1.peek() > array[i]) {
                p1.poll();
                p1.offer(array[i]);
            }
        }

        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = p1.poll();
        }
        return ret;
    }

    public static void main(String[] args) {

        int[] array = {1,12,3,41,5,16,7,81,9,10};
        int[] ret = topk(array, 5);
        System.out.println(Arrays.toString(ret));

    }
}
